import Vue from "vue"
import axios from "axios"
import VueAxios from "vue-axios"
import store from "@/store"
import router from "@/router"
import Qs from "qs"
import { Toast } from 'vant'
Vue.use(VueAxios, axios)
Vue.prototype.ajax = axios
//  axios.defaults.baseURL = "http://192.168.101.80:8080" // 本地
axios.defaults.baseURL = "http://182.92.172.108:8080" // 本地
// axios.defaults.baseURL = "http://api.jrybh.com" // line
axios.defaults.paramsSerializer = params => {
  return Qs.stringify(params, { indices: false })
}

Vue.axios.interceptors.response.use(response => {
  Toast.clear();
  if (response.data.code == 200) {
    return response.data
  } else {
    Toast.fail(response.data.msg)
    throw new Error()
    // return Promise.reject(new Error(response.data.msg))
  }
}, err => {
  Toast.clear();
  Toast.fail('请求错误')
  return Promise.reject(err)
})

axios.interceptors.request.use(config => {
  // 在发送请求之前做些什么
  config.headers = {
    ACCESS_TOKEN: localStorage.getItem('token')
  }
  Toast.loading({
    duration: 0,
    message: '加载中...',
  });
  return config
}, error => {
  // 对请求错误做些什么
  Toast.clear();
  return Promise.reject(error)
})

export { default as shoppingService } from "./shoppingService"